﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace HTComics.Data
{
    public class Common
    {
        public static DataTable GetDataTable(string sql)
        {
            DataTable dtResults = new DataTable();
            SqlConnection conn = new SqlConnection();
            SqlCommand cmd;
            SqlDataReader reader;

            try
            {
                conn.ConnectionString = ConfigurationManager.ConnectionStrings[ConfigurationManager.AppSettings["DBConn"]].ConnectionString;
                conn.Open();

                cmd = conn.CreateCommand();
                cmd.CommandText = sql;
                reader = cmd.ExecuteReader();

                if (reader.HasRows)
                {
                    dtResults.Load(reader);
                    dtResults.TableName = "Table1";
                }
            }
            catch (Exception ex)
            {
            }
            finally
            {
                conn.Close();
            }

            return dtResults;
        }

        public static DataSet GetDataSet(string sql)
        {
            DataSet dsResults = new DataSet();
            SqlDataAdapter objAdapter = new SqlDataAdapter();
            SqlConnection conn = new SqlConnection();

            try
            {
                conn.ConnectionString = ConfigurationManager.ConnectionStrings[ConfigurationManager.AppSettings["DBConn"]].ConnectionString;
                conn.Open();

                objAdapter = new SqlDataAdapter(sql, conn);
                objAdapter.Fill(dsResults);
            }
            catch (Exception ex)
            {
            }
            finally
            {
                conn.Close();
            }

            return dsResults;
        }
    }
}
